package service.impl;

import dao.GoodsDao;
import dao.impl.GoodsDaoImpl;
import entity.Goods;
import urity.Page;
import service.GoodsService;
import vo.FlowVo;
import vo.SalesVolumeVo;

import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    GoodsDao dao=new GoodsDaoImpl();

    /**
     * 查询
     * @param sql
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<Goods> selectAll(String sql,int pageNo, int pageSize) {
        return dao.selectAll(sql, pageNo,  pageSize);
    }

    /**
     * 分页查询
     * @param goodsName
     * @param sort
     * @param pageNo
     * @param pageSize
     * @param opr
     * @return
     */
    @Override
    public Page selectPage(String goodsName,String sort,int pageNo, int pageSize,String opr) {
        Page page=new Page();
        //状态
        String sql="";
       if (opr.equals("pagelist")){
            sql="select * from goods where state = 1 ";
        }else if (opr.equals("recycle")){
            sql=" select * from goods where  state = 0 ";
       }


       //判断排序和关键字查询
        if (goodsName!=null&&!goodsName.equals("")){
            sql+=" and goodsName like '%"+goodsName+"%'";
        }

        //排列方式
        if (sort!=null&&!sort.equals("")){
            if (sort.equals("price")){
                sql+=" ORDER BY `price`";
            }else if (sort.equals("time")){
                sql+=" ORDER BY `goodsTime` DESC";
            }
        }

                String sql2="select count(1) ";
                sql2+=updateSql(sql);

                //给分页对象个属性复制
                if(pageNo<=0){
                    pageNo=1;
                }
                page.setPageNo(pageNo);
                page.setPageSize(pageSize);
                //总记录数
                int count=0;
                count= dao.selectcount(sql2);
                page.setCountPage(count);
                //总页数
                int total=0;
                if (count%pageSize==0){
                    total=count/pageSize;
                }else{
                    total=count/pageSize+1;
                }
                page.setTotalPage(total);

                sql+="  LIMIT ?, ?";

            List<Goods> list = dao.selectAll( sql, pageNo, pageSize);
            page.setList(list);


        return page;
    }

    /**
     * 新增商品信息
     * @param goods
     * @return
     */
    @Override
    public int goodsinsert(Goods goods) {
        return dao.goodsinsert(goods);
    }

    @Override
    public String updateSql(String sql) {
       return sql.substring(sql.indexOf("*")+1);
    }

    @Override
    public int delect(int state,String time,int id) {
        return dao.delect(state,time,id);
    }

    @Override
    public Goods selectOne(int id) {
        return dao.selectOne(id);
    }

    @Override
    public int goodsdelect(int id) {
        return dao.goodsdelect(id);
    }

    @Override
    public int update(Goods gs) {
        return dao.update(gs);
    }

    @Override
    public List<SalesVolumeVo> selectSum() {
        return dao.selectSum();
    }

    @Override
    public List<FlowVo> selectFlow() {
        return dao.selectFlow();
    }

    @Override
    public int selectcount(String sql) {
        return dao.selectcount(sql);
    }
}

